package com.windfone.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.common.StringUtil;
import com.windfone.model.Software;
import com.windfone.service.SoftwareService;

public class DownloadAction extends ActionSupport {
	private SoftwareService softwareService;
	private int beginRecord;
	private String currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private Software bean;
	private String softwareId;
	private String softwareTypeId;
	private List<Software> softwareList;
 
	public String detail() throws Exception {
		if ( softwareId != null &&  !"".equals( softwareId )){
			if ( StringUtil.isInteger(  softwareId )){
				 List<Software> sf=	softwareService.getSoftwareById( Integer.parseInt(  softwareId ) );
				if ( sf.size()>0) {
					bean = sf.get(0);
				}
			}
		}
		return SUCCESS;
	}
	
	public String execute() throws Exception {
		String whereCondition = "";
		if ( softwareTypeId != null &&  !"".equals( softwareTypeId )){
			if ( StringUtil.isInteger(  softwareTypeId )){
					whereCondition = " AND s.softwareTypeId="+ softwareTypeId;
			}
		}  
		if ( currentPage == null ||  "".equals( currentPage )){
			currentPage = "0";
		}
		if ( StringUtil.isInteger(  currentPage )){
			PageUtil pu = new PageUtil();
			pu.setCurrentPage(Integer.parseInt(  currentPage ));
			int totalRecords = softwareService.getAllRecorderNumber(whereCondition) ;
			pu.setTotalRecords( totalRecords );
			pu.setPageSize(Constant.PAGE_SIZE);
			pu.dealPage();
			int currentPage2 = pu.getCurrentPage();
			nextPage = pu.getNextPage();
			totalPage = pu.getTotalPage();
			prevPage = pu.getPrevPage();
			
			beginRecord = Constant.PAGE_SIZE * (currentPage2 -1);
			if ( beginRecord< 0 )
				beginRecord = 0; 
			softwareList = softwareService.getAllSoftware(beginRecord,whereCondition);		
		}
		return SUCCESS;
	}
	public SoftwareService getSoftwareService() {
		return softwareService;
	}
	public int getBeginRecord() {
		return beginRecord;
	}
	public String getCurrentPage() {
		return currentPage;
	}
	public int getNextPage() {
		return nextPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public int getPrevPage() {
		return prevPage;
	}
	public Software getBean() {
		return bean;
	}
	public String getSoftwareId() {
		return softwareId;
	}
	public String getSoftwareTypeId() {
		return softwareTypeId;
	}
	public List<Software> getSoftwareList() {
		return softwareList;
	}
 
	public void setSoftwareService(SoftwareService softwareService) {
		this.softwareService = softwareService;
	}
	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}
	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}
	public void setBean(Software bean) {
		this.bean = bean;
	}
	public void setSoftwareId(String softwareId) {
		this.softwareId = softwareId;
	}
	public void setSoftwareTypeId(String softwareTypeId) {
		this.softwareTypeId = softwareTypeId;
	}
	public void setSoftwareList(List<Software> softwareList) {
		this.softwareList = softwareList;
	}
}
